import axios from 'axios'

const service = axios.create({
    baseURL: 'http://localhost:8081',
    //baseURL: 'http://localhost:8081http://47.109.70.92:8081',
    timeout: 5000
  })

export  const $post = async (url,params)=>{
    let {data} = await service.post(url,params)
    return data
}

export const $get = async (url,params)=>{
  let {data} = await service.get(url,{
      params
  })
  return data
}

//put 方法
export const $put = async (url,params)=>{
  let {data} = await service.put(url,params)
  return data
}

//delete 方法
export const $delete = async (url,params)=>{

  let {data} = await service.delete(url,params)
  return data
}

/*
 */
service.interceptors.response.use(
  response => {
    console.log(response)
    if(response.status == 200){
      //alert (response.data.msg)
      return response
    }else{
      //alert (response.data.msg)
      return -1;
    }
    
  },
  error => {
        alert ("拦截器")
    return Promise.reject(error)
  }
)

service.interceptors.request.use(config=>{
  if(localStorage.getItem('token')){
    const cleanedToken =localStorage.getItem('token');
    config.headers.token=cleanedToken;
  }
  return config
}, error => {
        
    return Promise.reject(error)
  }
)

  export default service